package com.dashu.device.service.JL;


import com.alibaba.fastjson.JSON;
import com.dashu.base.bean.device.JL.req.*;
import com.dashu.base.bean.device.JL.resp.JLDevicePicAddResp;
import com.dashu.base.bean.device.JL.resp.JLDevicePicListResp;
import com.dashu.base.enums.common.Status;
import com.dashu.base.enums.device.Iden;
import com.dashu.base.utils.CopyUtil;
import com.dashu.base.utils.ToEntityAnStr;
import com.dashu.base.utils.File.PicUtils;
import com.dashu.base.utils.UIDUtil;
import com.dashu.base.utils.http.HttpHelper;
import com.dashu.base.vo.DeviceOpenDoorReqVO;
import com.dashu.base.vo.DevicePicAddVO;
import com.dashu.base.vo.DevicePicDelReqVO;
import com.dashu.base.vo.DevicePicUpdReqVO;
import com.dashu.device.api.JL.JLExternalApi;
import com.dashu.device.dto.*;
import com.dashu.device.model.CompanyPersonal;
import com.dashu.device.model.CompanyTeacher;
import com.dashu.device.model.QCompanyPersonal;
import com.dashu.device.model.QCompanyTeacher;
import com.dashu.device.service.CompanyPersonalService;
import com.dashu.device.service.CompanyTeacherService;
import com.dashu.device.service.DataService;
import com.querydsl.core.BooleanBuilder;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.persistence.FlushModeType;
import javax.transaction.Transactional;
import java.io.UnsupportedEncodingException;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;

@Slf4j
@Service
public class JLInsideExternalService<T> extends DataService implements JLExternalApi<T> {

    @Autowired
    private CompanyTeacherService companyTeacherService;
    @Autowired
    private CompanyPersonalService companyPersonalService;

    @Override
    public DevicePicAddRespDTO<T> addUser(DevicePicAddVO<T> req) throws Exception {
        log.info("JLAdapter.addPic:" + req.getDeviceUUID());
//        JSONObject Json = JSONObject.parseObject(req.getResp());
//        JLDevicePicAddReq picAddReq = JSON.toJavaObject(Json, JLDevicePicAddReq.class);
        JLDevicePicAddReq picAddReq = new JLDevicePicAddReq();
        //将图片保存
        picAddReq.setName("WBListInfoREQ");
        picAddReq.setTimeStamp(System.currentTimeMillis());
        picAddReq.setMode(2);
        picAddReq.setAction("AddOnePic");
        AddOnePic addOnePic = new AddOnePic();
        addOnePic.setImg(PicUtils.image2Base64(req.getResp()));
        System.out.println(req.getResp());
        addOnePic.setImgNum(req.getUserId());
        addOnePic.setImgName(req.getUserName());
        picAddReq.setAddOnePic(addOnePic);
        String url = "http://" + req.getIp() + ":" + req.getPort() + "/";
        String resp = HttpHelper.post(url, null, new HashMap<>(16), JSON.toJSONString(picAddReq));
//        JLDevicePicAddResp jlDevicePicListResp = ToEntityAnStr.string2Obj(resp, JLDevicePicAddResp.class);
//        System.out.println(jlDevicePicListResp.toString());
        if (!StringUtils.isEmpty(resp)) {
            upStatus(req.getUserId());
            DevicePicAddRespDTO<T> addResp = new DevicePicAddRespDTO<>();
            addResp.setMsg("添加成功");
            log.info("CQAdapter2.addPic:");
            return addResp;
        }

        return null;
    }

    @Override
    public DevicePicDelRespDTO<T> delPic(DevicePicDelReqVO<T> req) throws UnsupportedEncodingException {
        log.info("JLAdapter.addPic:" + req.getDeviceUUID());
//        JSONObject Json = JSONObject.parseObject(req.getResp());
//        JLDevicePicDelReq picDelReq = JSON.toJavaObject(Json, JLDevicePicDelReq.class);
        JLDevicePicDelReq picDelReq = new JLDevicePicDelReq();
//        if (picDelReq == null) return null;
        //将图片保存
        picDelReq.setName("WBListInfoREQ");
        picDelReq.setTimeStamp(System.currentTimeMillis());
        picDelReq.setMode(2);
        picDelReq.setAction("DeleteOnePic");
        DeleteOnePic deleteOnePic = new DeleteOnePic();
        deleteOnePic.setLibIndex(0);
        deleteOnePic.setFlieIndex(0);
        deleteOnePic.setPic(req.getUserName());
        picDelReq.setDeleteOnePic(deleteOnePic);
//        JLDevicePicListResp jlDevicePicListResp = GetList();
        String url = "http://" + req.getIp() + ":" + req.getPort() + "/";
        String resp = HttpHelper.post(url, null, new HashMap<>(16), JSON.toJSONString(picDelReq));
//        JLDevicePicDelResp jlDevicePicAddResp = ToEntityAnStr.string2Obj(resp, JLDevicePicDelResp.class);
//        System.out.println(jlDevicePicAddResp.toString());
        if (!StringUtils.isEmpty(resp)) {
            DevicePicDelRespDTO<T> addResp = new DevicePicDelRespDTO<>();
            addResp.setMsg("删除成功");
            log.info("CQAdapter2.addPic:");
            return addResp;
        }
        return null;
    }

    @Override
    public DevicePicUpdRespDTO<T> updPic(DevicePicUpdReqVO<T> req) throws Exception {
        log.info("JLAdapter.addPic:" + req.getDeviceUUID());
//        JSONObject Json = JSONObject.parseObject(req.getResp());
//        JLDevicePicAddReq picAddReq = JSON.toJavaObject(Json, JLDevicePicAddReq.class);
        DevicePicDelReqVO devicePicDelReqVO = CopyUtil.copyEnty(req, DevicePicDelReqVO.class);
        devicePicDelReqVO.setUserName(devicePicDelReqVO.getUserName() + "_" + devicePicDelReqVO.getIden() + "_" + devicePicDelReqVO.getUserId());
        DevicePicAddVO devicePicAddVO = CopyUtil.copyEnty(req, DevicePicAddVO.class);
        devicePicAddVO.setUserId(devicePicAddVO.getIden() + "_" + devicePicAddVO.getUserId());
        DevicePicDelRespDTO devicePicDelRespDTO = delPic(devicePicDelReqVO);
        if (devicePicDelRespDTO != null) {
            DevicePicAddRespDTO devicePicAddRespDTO = addUser(devicePicAddVO);
            System.out.println(devicePicAddRespDTO);
        }


        return null;
    }

    @Override
    public DeviceOpenDoorRespDTO<T> openDoor(DeviceOpenDoorReqVO<T> req) throws UnsupportedEncodingException {
        log.info("JLAdapter.addPic:" + req.getDeviceUUID());
        JLDeviceRemoteOpenReq openReq = new JLDeviceRemoteOpenReq();
        if (openReq == null) return null;
        //将图片保存
        openReq.setName("remoteOpenDoorREQ");
        openReq.setUUID(req.getDeviceUUID());
        String url = "http://" + req.getIp() + ":" + req.getPort() + "/";
        String resp = HttpHelper.post(url, null, new HashMap<>(16), JSON.toJSONString(openReq));
        DeviceOpenDoorRespDTO deviceOpenDoorRespDTO = ToEntityAnStr.string2Obj(resp, DeviceOpenDoorRespDTO.class);
        return deviceOpenDoorRespDTO;
    }


    //    public JLDevicePicListResp GetList() throws UnsupportedEncodingException {
//        JLDevicePicListReq picListReq = new JLDevicePicListReq();
//        picListReq.setName("WBListInfoREQ");
//        picListReq.setMode(2);
//        picListReq.setAction("GetList");
//        picListReq.setTimeStamp(System.currentTimeMillis());
//        String resp = HttpHelper.post(jl_path, null, new HashMap<>(16), JSON.toJSONString(picListReq));
//        JLDevicePicListResp jlDevicePicListResp = ToEntityAnStr.string2Obj(resp, JLDevicePicListResp.class);
//        return jlDevicePicListResp;
//    }

    @Transactional
    public void upStatus(String userId) {
        List<String> pg = Arrays.asList(userId.split("_"));
        long userid = Long.parseLong(pg.get(1));
        if (Iden.STUDENT.name().equals(pg.get(0))) {
            companyPersonalService.upPersonalStatus(userid);
        } else {
            companyTeacherService.upTeacherStatus(userid);
        }

    }
}
